package comum;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import comum.InfoArquivo;

public class ListaArquivo {
	
	
	public ArrayList<String> listaArquivos(String path) {

		File dir = new File(path);

		if (!dir.isDirectory()) {
			System.out.println("No directory provided");
			return null;
		}

		ArrayList<String> arrArquivos = new ArrayList<String>();

		File[] files = dir.listFiles();

		FileFilter fileFilter = new FileFilter() {
			public boolean accept(File file) {
				return !file.isDirectory();
			}
		};

		files = dir.listFiles(fileFilter);

		for (File file : files) {
			arrArquivos.add(file.getName());
		}

		return arrArquivos;
	}

	public ArrayList<String> listaPastas(String path) {

		File dir = new File(path);

		if (!dir.isDirectory()) {
			System.out.println("No directory provided");
			return null;
		}

		ArrayList<String> arrPastas = new ArrayList<String>();

		File[] files = dir.listFiles();

		FileFilter fileFilter = new FileFilter() {
			public boolean accept(File file) {
				return file.isDirectory();
			}
		};
		files = dir.listFiles(fileFilter);

		for (File file : files) {
			arrPastas.add(file.getName());
		}
		return arrPastas;
	}

	public InfoArquivo getInfoArquivo(String path) {

		File file = new File(path);

		InfoArquivo info = new InfoArquivo();

		info.setUltimaMod(new Date(file.lastModified()));
		info.setTamanho(file.length());

		return info;
	}
	
	public void deleteArquivo(String path) {

		File file = new File(path);

		file.delete();
	}
	
	public void renameArquivo(String path,String newName)
	{
		File file = new File(path);
		File file2 = new File(newName);

		// Rename file (or directory)
		boolean success = file.renameTo(file2);
		if (!success) {
		    System.out.println("Erro ao renomear");
		}
	}
	
	public String listaArquivosString(String path) {
		ArrayList<String> listagem = this.listaArquivos(path);
		String lista = "";
		Iterator<String> iterator = listagem.iterator();
		while (iterator.hasNext()) {
		  String element = iterator.next();
		  lista += element+"\n";
		}
		return lista;
	}


	public String listaPastasString(String path) {
		ArrayList<String> folders = this.listaPastas(path);
		Iterator<String> iterator = folders.iterator();
		
		String text = "";
		
		while (iterator.hasNext()) {
		  String element = iterator.next();
		  text += element+"\n";
		}
		return text;
	}
}
